home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14101 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  65 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: Help to detect error 
  5. Message-ID: <DpqLyB.8J9@rci.ripco.com>
  6. X-Nntp-Sender: mambuhl@golden.ripco.com
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Fri, 12 Apr 1996 06:56:35 GMT
  10.  
  11. bz786@torfree.net (Sherif Asif) in <DpMA82.KEE.0.bloor@torfree.net> asks
  12. about several errors in his code.
  13.  
  14. Even though there have been some claims that everyone gets indentation
  15. "right" and somehow can't handle braces, the lack of indentation in your
  16. code is a clear counter-example.  The indentation has been added in the
  17. corrections below.  As usual, my changes (or other notes) are marked
  18. with `/* mha - ... */' comments:
  19.  
  20. #include <stdio.h>
  21. /*reoder 1 dim array, int array from smallest to largest*/
  22. int main()
  23. {                               /* mha - added explicit return type
  24.                                  * [optional] */
  25.     int i, n, x[100];
  26.     void reorder(int n, int x[]);   /* mha - added return type (so this
  27.                                  * is now a prototype rather than an
  28.                                  * erroneous attempt to invoke reorder()
  29.                                  */
  30.     printf("How many no:\n");
  31.     scanf("%d", &n);            /* mha - this is not a good idea.  For
  32.                                  * other choices, get the FAQ from
  33.                                  * rtfm.mit.edu and read it. */
  34.     printf("\n");
  35.     for (i = 0; i < n; ++i);
  36.     {
  37.         printf("i = %d  x = ", i + 1);
  38.         scanf("%d", &x[n]);     /* mha - see above */
  39.     }
  40.     reorder(n, x);
  41.     printf("Recorded list of nos:\n\n");
  42.     for (i = 0; i < n; ++i)
  43.         printf("i= %d  x=%d\n", i + 1, x[i]);
  44.     return 0;                   /* mha - added explicit return
  45.                                  * [optional] */
  46. }
  47.  
  48. void reorder(int n, int x[])
  49. {                               /* mha - added explicit return type */
  50.     int item, i, temp;
  51.     for (item = 0; item < n - 1; ++item)
  52.         for (i = item + 1; i < n; ++i)  /* mha - changed `/' to `)' */
  53.             if (x[i] < x[item]) {
  54.                 temp = x[item];
  55.                 x[item] = x[i];
  56.                 x[i] = temp;
  57.             }
  58.     return;
  59. }
  60.  
  61.                                                                                                                        
  62. --
  63. * Martin Ambuhl       net: mambuhl@ripco.com
  64. * Chicago, IL (USA)    
  65.